package com.atguigu.java;

/**
 * ClassName: StringIntern
 * Description:
 * Date: 2021/3/10 19:10
 * Company: TravelSky
 *
 * @author YangXin
 */
public class StringIntern {

    public static void main(String[] args) {

        String s = new String("1");
        s.intern();
        String s2 = "1";
        System.out.println(s == s2);

        String s3 = new String("1") + new String("1");
        s3.intern();//上面调用toString()方法中"ab"不在字符串常量池中..但是调用intern()方法会在常量池中生成"11"
        String s4 = "11";
        System.out.println(s3 == s4); //true

        String s5 = new String("1") + new String("1");
        String s6 = "11";
        s5.intern();
        System.out.println(s5 == s6); //false

    }
}
